import router from '@/router'
import { getToken } from "@/composables/auth"
import { toast, hideFullLoading, showFullLoading } from "@/composables/util"
import store from './store'

//全局前置守卫
router.beforeEach(async (to, from, next) => {
  //显示loding
  showFullLoading()
  const token = getToken()
  //没有登录,强制去登录
  if (!token && to.path != '/login') {
    toast("你还没登陆哥们~", "error")
    return next({ path: '/login' })
  }
  //防止重复登录
  if (token && to.path == '/login') {
    toast("别重复登录哥们~", "error")
    return next({ path: from.path ? from.path : '/' })
  }
  //如果用户登录，自动获取信息，存储在vuex
  if (token) {
    await store.dispatch("getinfo")
  }
  let title = (to.meta.title ? to.meta.title : '') + '-小李飞刀~'
  document.title = title
  next()
})

//全局后置守卫
router.afterEach((to, from) => {
  hideFullLoading()
})